﻿message(STATUS "CMAKE_TOOLCHAIN_FILE = ${CMAKE_TOOLCHAIN_FILE}")    # 使用一次变量以消除警告

# 将生成的库文件输出到cmake编译目录的libs文件夹下
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/libs)

# CUDA NVCC 时 CMAKE_CXX_COMPILER_ID 是空的
if(CMAKE_SYSTEM_NAME STREQUAL "Windows" AND (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" OR NOT CMAKE_CXX_COMPILER_ID))
    set(Windows_MSVC true)
endif()

if(Windows_MSVC)
    if (${OPTIMIZE_LEVEL} STREQUAL O0)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Od")
    elseif(${OPTIMIZE_LEVEL} STREQUAL O1)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /O1")
    elseif(${OPTIMIZE_LEVEL} STREQUAL O2)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /O2")
    else()
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Ox")
    endif ()
else()
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -${OPTIMIZE_LEVEL}")
endif ()

if (${CMAKE_HOST_SYSTEM_NAME} MATCHES "CYGWIN")
    if (${NEED_TO_OPEN_CUDA})
        option(SUPPORT_CUDA_GPUS "Support CUDA GPU or not" OFF)
    endif ()
else ()
    if (${NEED_TO_OPEN_CUDA})
        option(SUPPORT_CUDA_GPUS "Support CUDA GPU or not" ON)
    endif ()
endif ()

if (${SUPPORT_CUDA_GPUS})
    message("support CUDA")
    find_package(CUDA REQUIRED)
    set(CMAKE_CUDA_COMPILER /usr/local/cuda/bin/nvcc)
    enable_language(CUDA)
    set(CMAKE_CUDA_STANDARD 11)
    set(CMAKE_CUDA_ARCHITECTURES 60)  #for Tesla P100.
    add_definitions(-DSUPPORT_CUDA_GPUS)
    if (${CMAKE_BUILD_TYPE} AND ${CMAKE_BUILD_TYPE} STREQUAL "Debug")
        set(CMAKE_CUDA_FLAGS ${CMAKE_CUDA_FLAGS} "-g -G")  # if Debug, enable cuda-gdb
    endif ()
    if(Windows_MSVC)
        set(CMAKE_CUDA_FLAGS "-Xcompiler /wd4819")         # 忽略 936 文件编码警告
    endif()
else ()
    message("do NOT support CUDA")
    set(CMAKE_CXX_STANDARD 11)
endif ()

if (${SUPPORT_DYNAMIC_LIBRARY})
    message("support dynamic library")
    add_definitions(-DSUPPORT_DYNAMIC_LIBRARY)
else ()
    message("do NOT support dynamic library")
endif ()

#SkyDDS
if (SUPPORT_SKYDDS)
    add_definitions(-DSUPPORT_SKYDDS)
    set(DDS_LIBRARY_NAME Sky_DDSBridgeImpl)
    set(DDS_ROOT ${SKY_DDS}/SkyDDS)
    set(ACE_ROOT ${SKY_DDS}/ACE_wrappers)
    set(TAO_ROOT ${SKY_DDS}/ACE_wrappers/TAO)

    include_directories(${ACE_ROOT}
            ${TAO_ROOT}
            ${DDS_ROOT}
            ${TAO_ROOT}/orbsvcs)

    link_directories(${CMAKE_SOURCE_DIR}/SkyDDS/idl
            ${ACE_ROOT}/lib
            ${DDS_ROOT}/lib
            ${SKY_DDS}/bin)

    link_libraries(FooMsgCommon
            SkyDDS_monitor
            SkyDDS_Dcps
            TAO_PortableServer
            TAO_AnyTypeCode
            TAO
            ACE)
endif ()

#RTSB_DDS
if (SUPPORT_RTSB_DDS)
    set(DDS_LIBRARY_NAME rtsb_DDSBridgeImpl)

    include_directories(${CMAKE_SOURCE_DIR}/rtsbDDS/idl
            ${RTSB_DDS}/include/CPP_I/DDSInterface
            ${RTSB_DDS}/include/CPPI_I/DDSInterface/include
            ${RTSB_DDS}/include/OS/include
            ${RTSB_DDS}/include/KN/include
            ${RTSB_DDS}/include/DB/include
            ${RTSB_DDS}/include/CI/include
            ${RTSB_DDS}/include/UI/include
            ${RTSB_DDS}/include/UT/include
            ${RTSB_DDS}/examples/include
            ${RTSB_DDS}/include/C++/CPPI)
    
    link_directories(${RTSB_DDS}/lib/Debug/Linux/KylinSecOS/aarch64/)

    link_libraries(KN OS CPPI)
endif ()

#ZR_DDS
if (SUPPORT_ZRDDS)
    set(ZRDDS_ROOT ${ROOT_PATH}/othersupport/ZRDDS)
endif ()

function(add_subdirs)
    file(GLOB dirs RELATIVE ${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/*)
    foreach(dir ${dirs})
        if(IS_DIRECTORY ${CMAKE_SOURCE_DIR}/${dir} AND EXISTS ${CMAKE_SOURCE_DIR}/${dir}/CMakeLists.txt)
            # 用来区分用户目录
            file(READ ${CMAKE_SOURCE_DIR}/${dir}/CMakeLists.txt cmakefile)
            if (${cmakefile} MATCHES "SUPPORT_DYNAMIC_LIBRARY" AND ${cmakefile} MATCHES "SUPPORT_CUDA_GPUS")
                add_subdirectory(${dir})
                message(STATUS "add_subdirectory: ${dir}")
            endif()
        endif()
    endforeach()
endfunction()

function(get_all_targets var)
    set(targets)
    get_all_targets_recursive(targets ${CMAKE_SOURCE_DIR})
    set(${var} ${targets} PARENT_SCOPE)
endfunction()

macro(get_all_targets_recursive targets dir)
    get_property(subdirectories DIRECTORY ${dir} PROPERTY SUBDIRECTORIES)
    foreach(subdir ${subdirectories})
        get_all_targets_recursive(${targets} ${subdir})
    endforeach()

    get_property(_targets DIRECTORY ${dir} PROPERTY BUILDSYSTEM_TARGETS)
    foreach(_target ${_targets})
        if(TARGET ${_target})
            list(APPEND ${targets} ${_target})
        endif()
    endforeach()
endmacro()
